local extension = Package:new("guang")
extension.extensionName = "ats"

Fk:loadTranslationTable{
    ["guang"] = "光",
}

--奈克瑟斯
local lingyv = fk.CreateActiveSkill{
  name = "lingyv",
  anim_type = "control",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_num = 0,
  target_num = 0,
  
  target_filter = function(self, to_select, selected, user)
    return to_select ~= user.id and Fk:currentRoom():getPlayerById(to_select):getMark("@lingyv") == 0
  end,
  feasible = function(self, selected, selected_cards)
    return #selected > 0  
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local targets = {}
    if type(effect.tos) == "table" then
      for _, pid in ipairs(effect.tos) do
        if type(pid) == "number" then
          table.insert(targets, room:getPlayerById(pid))
        end
      end
    end
    
    if #targets > 0 then
      room:loseHp(player, 1, self.name)
      for _, p in ipairs(targets) do
        room:setPlayerMark(p, "@lingyv", 1)  
      end
    end
  end,
}
local lingyv_distance = fk.CreateDistanceSkill{
  name = "#lingyv_distance",
  correct_func = function(self, from, to)
    if (from:hasSkill("lingyv") and to:getMark("@lingyv") > 0) or
       (from:getMark("@lingyv") > 0 and to:hasSkill("lingyv")) or
       (from:getMark("@lingyv") > 0 and to:getMark("@lingyv") > 0) then
      return -99  
    end
    return 0
  end,
}
local lingyv_debuff = fk.CreateTriggerSkill{
  name = "#lingyv_debuff",
  events = {fk.DamageInflicted, fk.BeforeDrawCard},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@lingyv") > 0
  end,
  on_trigger = function(self, event, target, player, data)
    if event == fk.DamageInflicted then
      data.damage = data.damage + 1
    elseif event == fk.BeforeDrawCard then
      data.num = math.max(0, data.num - 1)
    end
  end,
}

local function hasLingyuMark(room)
  for _, p in ipairs(room.alive_players) do
    if p:getMark("@lingyv") > 0 then
      return true
    end
  end
  return false
end
local lingyv_prohibit = fk.CreateProhibitSkill{
  name = "#lingyv_prohibit",
  is_prohibited = function(self, from, to, player)
    local room = Fk:currentRoom()
    if not room then return false end
    if not hasLingyuMark(room) then return false end

    local from_has_mark = from:hasSkill("lingyv") or from:getMark("@lingyv") > 0
    local to_has_mark = to:hasSkill("lingyv") or to:getMark("@lingyv") > 0

    if from_has_mark then
      return not to_has_mark
    else
      return to_has_mark
    end
  end,
}
local lingyv_extra = fk.CreateTargetModSkill{
  name = "#lingyv_extra",
  residue_func = function(self, player, skill, scope, card, to)
    if not to then return 0 end
    if not player:hasSkill("lingyv") then return 0 end
    local to_has_mark = to:getMark("@lingyv") > 0
    
    if to_has_mark then
      return 999
    end
    return 0
  end,
}
local lingyv_clear = fk.CreateTriggerSkill{
  name = "#lingyv_clear",
  events = {fk.TurnStart},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill("lingyv")
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    local has_mark = false
    for _, p in ipairs(room:getAlivePlayers()) do
      if p:getMark("@lingyv") > 0 then
        has_mark = true
        break
      end
    end
    if has_mark then
      for _, p in ipairs(room:getAlivePlayers()) do
        if p:getMark("@lingyv") > 0 then
          room:setPlayerMark(p, "@lingyv", 0)
        end
      end
    end
  end,
}

Fk:addSkill(lingyv_distance)
Fk:addSkill(lingyv_debuff)
Fk:addSkill(lingyv_clear)  
Fk:addSkill(lingyv_prohibit)
Fk:addSkill(lingyv_extra)

lingyv:addRelatedSkill(lingyv_distance)
lingyv:addRelatedSkill(lingyv_debuff)
lingyv:addRelatedSkill(lingyv_clear)  
lingyv:addRelatedSkill(lingyv_prohibit)
lingyv:addRelatedSkill(lingyv_extra)

local naikesesi = General:new(extension, "guang_naikesesi", "ao_red", 5)
naikesesi:addSkill(lingyv) 

Fk:loadTranslationTable{
    ["guang_naikesesi"] = "奈克瑟斯",
    ["#guang_naikesesi"] = "守护万家灯火",
    ["designer:guang_naikesesi"] = "子右",    --设计者
    ["cv:guang_naikesesi"] = "官方",          --配音
    ["illustrator:guang_naikesesi"] = "？？", --画师
    ["lingyv"] = "领域",
    ["@lingyv"] = "领域",
    ["lingyv_distance"] = "领域",
    ["lingyv_debuff"] = "领域",
    ["lingyv_clear"] = "领域",
    ["lingyv_prohibit"] = "领域",
    ["lingyv_extra"] = "领域",
    [":lingyv"] = "出牌阶段限一次，你可以选择任意名其他角色，然后失去一点体力，令被选择的其他角色获得“领域”标记。你与所有有“领域”标记的角色：你们之间的距离视为1。有“领域”标记的角色受到的伤害+1，摸牌时摸牌数-1。若场上有“领域”标记，则你与有“领域”标记的角色使用牌只能指定你们为目标；没有“领域”标记的角色使用牌只能指定没有“领域”标记的角色为目标；你对有“领域”标记的角色使用牌无次数限制。你的回合开始时移除所有的“领域”标记。",
    ["$naikesesi"] = "光是纽带，它会被继承，并再度发光。"
}

--雷欧
local fenzhan = fk.CreateTriggerSkill{
  name = "fenzhan",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.DrawNCards},
  on_use = function(self, event, target, player, data)
    data.n = data.n + player:getLostHp()
  end,
}
local fenzhan_multipleslash = fk.CreateTargetModSkill{
  name = "#fenzhan_multipleslash",
  anim_type = "offensive",
  residue_func = function(self, player, skill, scope)
    if player:getLostHp() > 0 and scope == Player.HistoryPhase and player:hasSkill("fenzhan") then
      return player:getLostHp() - 1
    end
    return 0
  end,
} 
local fenzhan_draw = fk.CreateTriggerSkill{
  name = "#fenzhan_draw",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if not (target == player and player:hasSkill("fenzhan") and player.phase == Player.Finish) then
      return false
    end
    return #player.room.logic:getEventsOfScope(GameEvent.Damage, 1, function(e)
      local damage = e.data[1]
      return damage.from == player
    end, Player.HistoryTurn) > 0
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, self.name)
  end,
}

local feiti = fk.CreateViewAsSkill{
  name = "feiti",
  anim_type = "offensive",
  pattern = "slash",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isProhibited(nil, Fk:cloneCard("slash"))
  end,
  card_filter = function(self, to_select, selected)
    local card = Fk:getCardById(to_select)
    return #selected == 0 and (
      Fk:currentRoom():getCardArea(to_select) == Card.PlayerHand or 
      Fk:currentRoom():getCardArea(to_select) == Card.PlayerEquip
    )
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return nil end
    local slash = Fk:cloneCard("slash")
    slash.skillName = self.name
    slash:addSubcard(cards[1])
    return slash
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isProhibited(nil, Fk:cloneCard("slash"))
  end,
}
local feiti_distance = fk.CreateTargetModSkill{
  name = "#feiti_distance",
  distance_limit_func = function(self, player, skill, card, to)
    if card and card.trueName == "slash" and table.contains(card.skillNames, "feiti") then
      return 1000
    end
    return 0
  end,
}
local feiti_trigger = fk.CreateTriggerSkill{
  name = "#feiti_trigger",
  mute = true,
  events = {fk.TargetSpecified, fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TargetSpecified then
      return target == player and player:hasSkill(feiti) and 
        data.card.trueName == "slash" and table.contains(data.card.skillNames, feiti.name) and
        data.firstTarget
    else 
      if player ~= target then return false end
      local card_event = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
      return card_event and card_event.feiti_armor
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.CardUseFinished then return true end
    if player.hp > 1 then
      return player.room:askForSkillInvoke(player, self.name, data, "#feiti-ignore-armor")
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUseFinished then
      for _, p in ipairs(room.alive_players) do
        room:removePlayerMark(p, fk.MarkArmorNullified)
      end
      return
    end
    local card_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
    room:loseHp(player, 1, self.name)
    room:addPlayerMark(room:getPlayerById(data.to), fk.MarkArmorNullified)
    card_event.feiti_armor = true 
  end,
}

Fk:addSkill(fenzhan_multipleslash)
Fk:addSkill(fenzhan_draw)
Fk:addSkill(feiti_distance)
Fk:addSkill(feiti_trigger)
feiti:addRelatedSkill(feiti_distance)
feiti:addRelatedSkill(feiti_trigger)
fenzhan:addRelatedSkill(fenzhan_multipleslash)
fenzhan:addRelatedSkill(fenzhan_draw)

local leiou = General:new(extension, "guang_leiou", "ao_red", 4)
leiou:addSkill(fenzhan)
leiou:addSkill(feiti)

Fk:loadTranslationTable{
  ["guang_leiou"] = "雷欧",
  ["#guang_leiou"] = "孤独的雄狮",
  ["designer:guang_leiou"] = "子右",
  ["cv:guang_leiou"] = "官方",
  ["illustrator:guang_leiou"] = "？？",
  ["fenzhan"] = "奋战",
  [":fenzhan"] = "锁定技，摸牌阶段你可以多摸X张牌（X为你已损失的体力值）。出牌阶段，你可以多出X-1张【杀】。结束阶段，若本回合你造成过伤害，你摸一张牌。",
  ["fenzhan_multipleslash"] = "奋战",
  ["fenzhan_draw"] = "奋战",
  ["feiti"] = "飞踢",
  [":feiti"] = "出牌阶段限一次，你可以弃置一张牌视为使用一张无视距离的【杀】。你可以失去1点体力，令此【杀】无视防具。",
  ["#feiti-choose"] = "飞踢：选择一名角色作为【杀】的目标",
  ["#feiti-ignore-armor"] = "飞踢：你可以失去1点体力，令此【杀】无视防具",
  ["#feiti_trigger"] = "飞踢",
}



return extension
